package com.lanou.bookstore.order.web.servlet;

import com.lanou.bookstore.order.domain.Order;
import com.lanou.bookstore.order.domain.Transit;
import com.lanou.bookstore.order.service.OrderSercice;
import com.lanou.bookstore.order.service.OrderSerciceImpl;
import com.lanou.bookstore.user.dao.UserDao;
import com.lanou.bookstore.user.dao.UserDaoImpl;
import com.lanou.bookstore.user.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/oas")
public class OrderAllServlet extends HttpServlet {

    UserDao ud = new UserDaoImpl();
    User user = new User();
    OrderSercice os = new OrderSerciceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String uid = null;

        // 1.准备订单表(Order)数据
        String uname = (String) req.getSession().getAttribute("uname");
        user.setUsername(uname);
        List<User> userList = ud.findByUsername(user);
        for (User user1 : userList) {
            // 谁买的    user的id
            uid = user1.getUid();
        }
        List<Order> orderList = os.queryAll(uid);


        // 2.准备订单项表(OrderItem) 和 图书表(book)数据  =  Transit

        List<Transit> transitList = new ArrayList<Transit>();

        // 2.1   通过oid查询
        for (Order order : orderList) {
            List<Transit> transitListTemp = new ArrayList<Transit>();
            transitListTemp = os.queryTransit(order.getOid());

            for (Transit transit : transitListTemp) {
                transitList.add(transit);
            }
        }


        req.getSession().setAttribute("orderList",orderList);
        req.getSession().setAttribute("transitList",transitList);
        req.getRequestDispatcher("/jsps/order/list.jsp").forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
